package com.pengheng.wc;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class SocketStreamWordCount {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // IDEA运行时，提供webui，一般用于本地测试
        //需要引入依赖 flink-runtime-web
        // idea运行，不指定并行度，默认就是电脑的线程数
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        DataStreamSource<String> lineDs = env.socketTextStream("192.168.10.100", 7777);
        SingleOutputStreamOperator<Tuple3<String, Long, String>> sum = lineDs
                .flatMap((FlatMapFunction<String, Tuple3<String, Long, String>>) (line, out) -> {
                    String[] words = line.split(" ");

                    for (String word : words) {
                        out.collect(Tuple3.of(word, 1L, "hello"));
                    }
                })
                .returns(Types.TUPLE(Types.STRING, Types.LONG, Types.STRING))
                .keyBy(data -> data.f0 + "-" + data.f2)
                .sum(1);

        sum.print();

        env.execute();

    }
}
